package org.ljy.forum6.services.impl;

import lombok.extern.slf4j.Slf4j;
import org.ljy.forum6.common.AppResult;
import org.ljy.forum6.common.ResultCode;
import org.ljy.forum6.dao.BoardMapper;
import org.ljy.forum6.exception.ApplicationException;
import org.ljy.forum6.model.Board;
import org.ljy.forum6.services.IBoardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //1.校验
        if(num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用dao层
        List<Board> list = boardMapper.selectByNum(num);
        return list;
    }

    @Override
    public Board selectById(Long id) {
        //1.参数验证
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用dao
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getState()==1) {
            log.warn("版块不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        return board;
    }

    /**
     * 版块文章数目+1 （发布文章）
     */
    @Override
    public void addOneArticleCountById(Long id) {
        //1.参数验证
        if(id<=0) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //2.根据版块id查询该作者
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //3.创建新对象进行修改
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()+1);
        //4.调用dao层
        boardMapper.updateByPrimaryKeySelective(updateBoard);
    }

    /**
     * 删除帖子操作--版块下对应文章数-1
     */
    @Override
    public void subOneArticleCountById(Long id) {
        //1.参数校验
        if(id==null || id<=0) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //2.查询版块是否存在
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null || board.getDeleteState()==1) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //3.判断用户版块数量是否合法
        if(board.getArticleCount()<=0) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //4.更新版块文章数量
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()-1);
        //5.调用dao
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if( row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
    }
}
